package com.compus_help.controller.admin;


import com.compus_help.common.PageResult;
import com.compus_help.common.Result;
import com.compus_help.entity.DTO.*;
import com.compus_help.entity.VO.AdminLoginVO;
import com.compus_help.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/admin")

public class AdminController {

    private static final Logger log = LoggerFactory.getLogger(AdminController.class);
    @Autowired
    public AdminService adminService;
    //管理员登录
    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLogindto){
        log.info("管理员登录");
        return Result.success(adminService.adminLogin(adminLogindto));
    }
    /**
     * 添加学生用户
     * @param studentDTO
     * @return
     */
    @PostMapping("/addstudents")
    public Result addStudent(@RequestBody studentDTO studentDTO){
        try {
            adminService.addStudent(studentDTO);
            log.info("添加学生用户成功");
            return Result.success();
        } catch (Exception e) {
            log.error("添加学生用户失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 批量删除学生用户
     * @param studentIds
     * @return
     */
    @DeleteMapping("/students")
    public Result deleteStudents(List<String> studentIds){
        try {
            adminService.deleteStudents(studentIds);
            log.info("批量删除学生用户成功");
            return Result.success();
        } catch (Exception e) {
            log.error("批量删除学生用户失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 分页查询学生用户
     * @param studentPageDTO
     * @return
     */
    @PostMapping("/students/page")
    public Result<PageResult> getStudents(@RequestBody studentPageDTO studentPageDTO){
        try {
            //控制台输出学号，姓名，班级的条件查询
            System.out.println(studentPageDTO.getCampusCardNumber());
            System.out.println(studentPageDTO.getStudentName());
            System.out.println(studentPageDTO.getClassName());
            PageResult pageResult = adminService.getStudents(studentPageDTO);
            log.info("分页查询学生用户成功");
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("分页查询学生用户失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 接单权限修改
     * @param studentId
     * @return
     */
    @PutMapping("/student/acceptOrder")
    public Result updateAcceptOrder(@RequestParam String studentId){
        try {
            adminService.updateAcceptOrder(studentId);
            log.info("更新学生用户接单权限成功");
            return Result.success();
        } catch (Exception e) {
            log.error("更新学生用户接单权限失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 发单权限修改
     * @param studentId
     * @return
     */
    @PutMapping("/student/publishOrder")
    public Result updatePublishOrder(@RequestParam String studentId){
        try {
            adminService.updatePublicOrder(studentId);
            log.info("更新学生用户发单权限成功");
            return Result.success();
        } catch (Exception e) {
            log.error("更新学生用户发单权限失败", e);
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("student/info")
//修改学生信息
    public Result updateStudentInfo(@RequestBody studentDTO studentDTO){
        try {
            System.out.println(studentDTO.getCanAcceptOrder());
            adminService.updateStudentInfo(studentDTO);
            log.info("更新学生用户信息成功");
            return Result.success();
        } catch (Exception e) {
            log.error("更新学生用户信息失败", e);
            return Result.error(e.getMessage());
        }
    }


}

